package com.example.event_resource_java.authority.server.sms;


import com.example.event_resource_java.commons.enums.ApiEnumError;
import com.example.event_resource_java.commons.util.EhCacheUtil;
import com.example.event_resource_java.sms.config.SmsBasicConfigProperties;
import com.example.event_resource_java.sms.support.SmsSender;
import com.example.event_resource_java.sms.support.SmsVerify;
import com.example.event_resource_java.starinside.exception.IFastApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Random;

/**
 * <pre>
 *
 * </pre>
 * <small> 2018/8/31 16:45 | Aron</small>
 */
@Slf4j
@Service("SmsManager")
public class SmsManager implements SmsVerify {

    private SmsSender smsSender;
    private SmsBasicConfigProperties properties;
    private Cache cache;

    public SmsManager(){}

    public SmsManager(SmsSender sender, SmsBasicConfigProperties properties, Cache cache){
        this.smsSender = sender;
        this.properties = properties;
        this.cache = cache;

    }

    //登录验证码
    public void send(String prefix, String mobile,String smsDemo) {
        String code = getCode();

        //发送手机短信
        smsSender.send(mobile, code, smsDemo);
        if (StringUtils.isNotBlank(code)) {
            String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + mobile;
            // 加入缓存
            if(log.isDebugEnabled()){
                log.debug("【SMS】cache.put({}, {})", properties.getCacheKey() + ":" + key, code);
            }
            cache.put(key, code);
        }
    }

    //企业注册验证码
    public void enterpriseSend(String prefix, String mobile,String smsDemo) {
        String code = getCode();

        //发送手机短信
        smsSender.send(mobile, code, smsDemo);
        if (StringUtils.isNotBlank(code)) {
          //  String key = "enAdd" + ":" +  prefix + ":" + mobile;
            String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + mobile;
            // 加入缓存
            if(log.isDebugEnabled()){
                log.debug("【SMS】cache.put({}, {})", properties.getCacheKey() + ":" + key, code);
            }
            cache.put(key, code);
        }
    }
    //注册验证码
    public void sendreg(String prefix, String mobile,String smsDemo) {
        String code = getCode();


        //发送手机短信
        smsSender.send(mobile, code, smsDemo);
        if (StringUtils.isNotBlank(code)) {
            String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + mobile;
            // 加入缓存
            if(log.isDebugEnabled()){
                log.debug("【SMS】cache.put({}, {})", properties.getCacheKey() + ":" + key, code);
            }
            cache.put(key, code);
            EhCacheUtil.put(mobile+"wx", code);
        }
    }

    //修改密码验证码
    public void sendPassword(String prefix, String mobile,String smsDemo) {
        String code = getCode();

        //发送手机短信
        smsSender.send(mobile, code, smsDemo);
        if (StringUtils.isNotBlank(code)) {
            String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + mobile;
            // 加入缓存
            if(log.isDebugEnabled()){
                log.debug("【SMS】cache.put({}, {})", properties.getCacheKey() + ":" + key, code);
            }
            cache.put(key, code);
        }
    }

    public void send(String mobile, String templateCode,Map templateParam){
        //发送手机短信
        smsSender.send(mobile, templateCode, templateParam);
    }

    public void send1(String mobile, String templateCode,String signName,Map messageContent) {
        smsSender.send(mobile,templateCode,signName,messageContent);

//        if (StringUtils.isNotBlank(code)) {
//            String key = properties.getCacheKeyPrefix() + ":" + mobile;
//
//            // 加入缓存
//            if(log.isDebugEnabled()){
//                log.debug("【SMS】cache.put({}, {})", properties.getCacheKey() + ":" + key, code);
//            }
//            cache.put(key, code);
//        }
    }

    @Override
    public void verify(String prefix, String mobile, String code) {
        String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + mobile;
//        if (!mobile.equals("18711358618")){
            Cache.ValueWrapper valueWrapper = cache.get(key);
            if (valueWrapper == null || !valueWrapper.get().equals(code)) {
                throw new IFastApiException(ApiEnumError.apiSmsCodeInvalid.getCodeStr());
            }
            // 移除缓存
            if(log.isDebugEnabled()){
                log.debug("【SMS】cache.evict({}, {})", properties.getCacheKey() + ":" + key, code);
            }
//        }
        cache.evict(key);
    }

    @Override
    public boolean verify1(String prefix, String mobile, String code) {
        String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + mobile;
//        if (!mobile.equals("18711358618")){
        Cache.ValueWrapper valueWrapper = cache.get(key);
        if (valueWrapper == null || !valueWrapper.get().equals(code)) {
           return false;
        }
        // 移除缓存
        if(log.isDebugEnabled()){
            log.debug("【SMS】cache.evict({}, {})", properties.getCacheKey() + ":" + key, code);
        }
//        }
        cache.evict(key);
        return true;
    }


    @Override
    public boolean loginVerify(String prefix, String phone, String code) {
        String key = properties.getCacheKeyPrefix() + ":" +  prefix + ":" + phone;
        Cache.ValueWrapper valueWrapper = cache.get(key);
        if (valueWrapper == null || !valueWrapper.get().equals(code)) {
            return false;
        }

        // 移除缓存
        if(log.isDebugEnabled()){
            log.debug("【SMS】cache.evict({}, {})", properties.getCacheKey() + ":" + key, code);
        }
        cache.evict(key);
        return true;
    }

    /**
     * <pre>
     * 生成随机码
     * </pre>
     */
    public String getCode(){
        return new Random().nextInt(8999) + 1000 + "";
    }

}
